package main

import (
	"encoding/json"
	"flag"
	"math"
	"strconv"
	"strings"
)

// Message 结构体-json的内容拆分
type Message struct {
	Gstatus string  `json:"status"`
	Alerts  []Als   `json:"alerts"`
	ComLab  ComLab  `json:"commonLabels"`
	ComAnno ComAnno `json:"commonAnnotations"`
	//  Receiver string `json:"receiver"`
	//  Glab Glab `json:"groupLabels"`
}

// ComLab 分组告警json
type ComLab struct {
	Calertname  string `json:"alertname"`
	Ccluster    string `json:"cluster"`
	Cservice    string `json:"service"`
	Cproject    string `json:"project"`
	Cinstance   string `json:"instance"`
	Cgroup      string `json:"group"`
	Crole       string `json:"role"`
	Cmoudle     string `json:"moudle"`
	Cmountpoint string `json:"mountpoint"`
}

// ComAnno 分组注释json
type ComAnno struct {
	Cdesc  string `json:"description"`
	Csumm  string `json:"summary"`
	Cvalue string `json:"value"`
}

// Als 告警json
type Als struct {
	Astatus string `json:"status"`
	Labels  Labels `json:"labels"`
	Annot   Annot  `json:"annotations"`
}

// Labels 告警Labels json
type Labels struct {
	Alertname  string `json:"alertname"`
	Instance   string `json:"instance"`
	Project    string `json:"project"`
	Service    string `json:"service"`
	Cluster    string `json:"cluster"`
	Group      string `json:"group"`
	Role       string `json:"role"`
	Moudle     string `json:"moudle"`
	Mountpoint string `json:"mountpoint"`
}

// Annot 告警Annot json
type Annot struct {
	Desc  string `json:"description"`
	Summ  string `json:"summary"`
	Value string `json:"value"`
}

// Constr 用于判断聚合项是否为空，不为空拼接说明
func Constr(Jsval, Explain string) (Unionval string) {
	if Jsval == "" {
		Unionval = ""
	} else {
		Unionval = Explain + ":" + Jsval + "\n"
	}
	return Unionval
}

// Constrsub 用于判断是否为空，不为空拼接说明，为了显示效果，抛弃超过50的内容
func Constrsub(NC, Jsvalsub, Explainsub string) (Unionvalsub string) {
	if NC == "" && Jsvalsub != "" && len(Jsvalsub) < 50 {
		Unionvalsub = Explainsub + ":" + Jsvalsub + "\n"
	} else {
		Unionvalsub = ""
	}
	return Unionvalsub
}

// Jsondata 函数用于处理消息body体里的内容，解析json
func Jsondata(data []byte) string {

	flag.Parse()
	Acc, _ := strconv.ParseFloat(*Accuracy, 64)
	// 解析json
	b := Message{}
	json.Unmarshal([]byte(data), &b)
	//fmt.Println(b)
	// N 开头的变量用于分组变量
	NCmountpoint, NCrole, NCmoudle, NCalertname, NCservice, NCcluster, NCgroup := "", "", "", "", "", "", ""
	NCvalue, NCproject, NCinstance, NCsumm, NCdesc := "", "", "", "", ""
	// S 开头的用于单个告警
	Svalue, SGroup, SStatus, SAlertname, SInstance, SProject, SService, SCluster := "", "", "", "", "", "", "", ""
	Smountpoint, Srole, Smoudle, Smes, SDesc, SSumm := "", "", "", "", "", ""

	// 重新格式化需要的内容

	NCalertname = Constr(b.ComLab.Calertname, "告警名称")
	NCservice = Constr(b.ComLab.Cservice, "服务")
	NCcluster = Constr(b.ComLab.Ccluster, "集群")
	NCmountpoint = Constr(b.ComLab.Cmountpoint, "挂载")
	NCproject = Constr(b.ComLab.Cproject, "项目")
	NCinstance = Constr(b.ComLab.Cinstance, "实例")
	NCgroup = Constr(b.ComLab.Cgroup, "分组")
	NCrole = Constr(b.ComLab.Crole, "角色")
	NCmoudle = Constr(b.ComLab.Cmoudle, "模块")
	NCdesc = Constr(b.ComAnno.Cdesc, "描述")
	NCsumm = Constr(b.ComAnno.Csumm, "说明")

	if b.ComAnno.Cvalue == "" {
		NCvalue = ""
	} else {
		fl, _ := strconv.ParseFloat(b.ComAnno.Cvalue, 64)
		f2 := math.Round(fl*Acc) / Acc
		s1 := strconv.FormatFloat(f2, 'f', -1, 64)
		NCvalue = "结果:" + s1 + "\n"
	}

	//获取每个实例不同的信息
	ins := 0
	for _, val := range b.Alerts {
		//fmt.Printf("b.Alerts %d is :%s\n",k,val )
		//fmt.Printf("%d",k)
		SAlertname = Constrsub(NCalertname, val.Labels.Alertname, "名称")
		SInstance = Constrsub(NCinstance, val.Labels.Instance, "实例")
		SProject = Constrsub(NCproject, val.Labels.Project, "项目")
		NCservice = Constrsub(NCservice, val.Labels.Service, "服务")
		SGroup = Constrsub(NCgroup, val.Labels.Group, "分组")
		Srole = Constrsub(NCrole, val.Labels.Role, "角色")
		Smoudle = Constrsub(NCmoudle, val.Labels.Moudle, "模块")
		Smountpoint = Constrsub(NCmountpoint, val.Labels.Mountpoint, "挂载")
		SDesc = Constrsub(NCdesc, val.Annot.Desc, "描述")
		SSumm = Constrsub(NCsumm, val.Annot.Summ, "说明")
		//fmt.Printf("%T", val.Annot.Value)
		//fmt.Println(val.Annot.Value)
		if NCvalue == "" && val.Annot.Value != "" {
			nf1, _ := strconv.ParseFloat(val.Annot.Value, 64)
			nf2 := math.Round(nf1*Acc) / Acc
			//nf2 := math.Round(nf1*1000) / 1000
			s2 := strconv.FormatFloat(nf2, 'f', -1, 64)
			Svalue = "结果:" + s2 + "\n"
		} else {
			Svalue = ""
		}

		SStatus = "状态:" + val.Astatus + "\n"
		Smes += SInstance + SStatus + SAlertname + SProject + SService + SCluster + SGroup + SDesc + SSumm + Smountpoint + Srole + Smoudle + Svalue
		ins = ins + 1
	}
	// Cmes 用于使用消息
	Cmes := "[" + strings.ToUpper(b.Gstatus) + "]"
	Cmes += NCalertname + NCservice + NCcluster + NCproject + NCgroup + NCmountpoint + NCrole + NCmoudle + NCvalue
	Cmes += NCinstance
	// Canno 用于合并注释
	Canno := NCdesc + NCsumm
	Mess := ""
	if ins > 1 {
		Mess = Cmes + "[" + Smes + "]" + Canno
	} else {
		Mess = Cmes + Canno
	}

	//fmt.Println(Mess)
	//fmt.Println(ins)
	return Mess
}
